/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;

import beans.StoreEmployeeBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Equipo SAHD
 */
public class StoreEmployeeDAO extends AbstractDAO<StoreEmployeeBean> {

    
    @Override
    public void insert(StoreEmployeeBean employee) throws DAOException {
         try {
                dataBaseManager.openConnection();
                String sqlStatement = "INSERT INTO STORE_EMPLOYEE(userId,storeId)values('" + employee.getUserId() + "','" + employee.getStoreId() + "');";
                dataBaseManager.executeUpdate(sqlStatement);
         }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
         } finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
        }
    }

    @Override
    public void delete(String condition) throws DAOException {
        try {
              dataBaseManager.openConnection();
              String sqlStatement = "DELETE FROM store_employee " + condition;
              dataBaseManager.executeUpdate(sqlStatement);    
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void update(StoreEmployeeBean employee, String condition) throws DAOException {
       try{
            dataBaseManager.openConnection();
            String sqlStatement = "UPDATE store_employee SET userId='" + employee.getUserId() + "',storeId='" + employee.getStoreId() + "' " + condition;
            dataBaseManager.executeUpdate(sqlStatement);
       }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
       } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
       }
    }


    @Override
    protected ArrayList<StoreEmployeeBean> searchAll(String condition) throws DAOException {
      try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE_EMPLOYEE ";
            ArrayList<StoreEmployeeBean> storeEmployeeBeanList = null;
            storeEmployeeBeanList = createStoreEmployeeBeanList(dataBaseManager.executeQuery(sqlStatement));
            return storeEmployeeBeanList;
      }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
      } finally{
            try {
                  dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                  throw new DAOException(exception.getExceptionMessage(),exception);
            }
      }
   }
    

    @Override
   protected StoreEmployeeBean search(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE_EMPLOYEE ";
            StoreEmployeeBean storeEmployeeBean = null;
            storeEmployeeBean = createStoreEmployeeBean(dataBaseManager.executeQuery(sqlStatement));
            return storeEmployeeBean;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

     private ArrayList<StoreEmployeeBean> createStoreEmployeeBeanList(ResultSet queryResult) throws DAOException{
        try{
            ArrayList<StoreEmployeeBean> storeEmployeeBeanList = new ArrayList();
            while(queryResult.next()){
                storeEmployeeBeanList.add((StoreEmployeeBean) beansGenerator.generateBean(queryResult, StoreEmployeeBean.class));
        }
            return storeEmployeeBeanList;
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
            throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    private StoreEmployeeBean createStoreEmployeeBean(ResultSet queryResult) throws DAOException{
        try{
            moveQueryResultToBegin(queryResult);
            return (StoreEmployeeBean) beansGenerator.generateBean(queryResult, StoreEmployeeBean.class);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
            throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    public void deleteStoreEmployee(int storeEmployeeId) throws DAOException{
        delete("WHERE storeEmployee='"+ storeEmployeeId+"'");
    }
    public StoreEmployeeBean searchStoreEmployee(int storeEmployeeId) throws DAOException{
        return search("WHERE storeEmployee='"+ storeEmployeeId+"'");
    }



     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }




}
